<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\model;
use think\facade\Db;
use think\facade\Log;

class Payorder {

	//创建支付订单-租赁
	public static function createorder3($aid, $bid, $mid, $type, $orderid, $ordernum, $title, $money, $score = 0) {
		$data = [];
		$data['aid'] = $aid;
		$data['bid'] = $bid;
		$data['mid'] = $mid;
		$data['orderid'] = $orderid;
		$data['ordernum'] = $ordernum;
		$data['title'] = $title;
		$data['money'] = $money;
		$data['score'] = $score;
		$data['type'] = $type; //shop collage scoreshop kanjia seckill recharge designerpage form
		$data['createtime'] = time();
		$id = Db::name('payorder')->insertGetId($data);
		if ($type == 'shop_hb') {
			Db::name('shop_order')->where('ordernum', 'like', $ordernum . '%')->update(['payorderid' => $id]);
		} else {
			Db::name($type . '_order')->where('id', $orderid)->update(['payorderid' => $id, 'type' => 1]);
		}
		return $id;
	}

	//创建支付订单
	public static function createorder($aid, $bid, $mid, $type, $orderid, $ordernum, $title, $money, $score = 0) {
		$data = [];
		$data['aid'] = $aid;
		$data['bid'] = $bid;
		$data['mid'] = $mid;
		$data['orderid'] = $orderid;
		$data['ordernum'] = $ordernum;
		$data['title'] = $title;
		$data['money'] = $money;
		$data['score'] = $score;
		$data['type'] = $type; //shop collage scoreshop kanjia seckill recharge designerpage form
		$data['createtime'] = time();
		$id = Db::name('payorder')->insertGetId($data);
		if ($type == 'shop_hb') {
			Db::name('shop_order')->where('ordernum', 'like', $ordernum . '%')->update(['payorderid' => $id]);
		} else {
			Db::name($type . '_order')->where('id', $orderid)->update(['payorderid' => $id]);
		}
		return $id;
	}
	//修改订单
	public static function updateorder($id, $newOrdernum, $newprice, $updateOrderId = 0) {

//        // 查询订单原价
		//        $order_info = Db::name('shop_order')->where('aid',aid)->where('id', $orderid)->find();
		//        // 查询订单原来应该支付金额
		//        $pay_money = Db::name('payorder')->where('aid',aid)->where('id', $order_info['payorderid'])->value('money');
		//        // 计算修改差价
		//        $order_num = explode('_', $order_info['ordernum'])[0] ?? $order_info['ordernum'] ; // 原支付订单号
		//        // 查询同笔订单信息
		//        $shop_order_info = Db::name('shop_order')->where('aid',aid)->where('ordernum', 'like',  $order_num . '%' )->select()->toArray();
		//
		//        // print_r($shop_order_info);die;
		//
		//        foreach ($shop_order_info as $k=>$v){
		//            // 修改订单号
		//            $new_order_num = str_replace($order_num, $newordernum, $v['ordernum']) ;
		//            $update_arr = [ 'ordernum'=>$new_order_num  ] ;
		//            // 只有当前修改的订单改价 其它子订单只修改单号 解决支付出现重复单号-金额不同问题
		//            if($v['id'] == $orderid){
		//                $update_arr['totalprice'] = $newprice ; // 后台改动后金额
		//            }
		//            Db::name('shop_order')->where('aid',aid)->where('id',$v['id'])->update($update_arr);
		//            Db::name('shop_order_goods')->where('aid',aid)->where('orderid',$v['id'])->update(['ordernum'=>$new_order_num]);
		//            // 修改子支付订单 订单号
		//            Db::name('payorder')->where('ordernum', $v['ordernum'])->update(['ordernum'=>$new_order_num]);
		//        }
		//
		//        // 计算差价 修改 总支付订单差价
		//        if($order_info['totalprice'] > $newprice){
		//            $newprice = bcsub($pay_money, bcsub($order_info['totalprice'], $newprice, 2), 2) ;
		//        }else{
		//            $newprice = bcadd($pay_money, bcsub($newprice, $order_info['totalprice'], 2), 2) ;
		//        }

		$payorder = Db::name('payorder')->where('id', $id)->find();
		if (in_array($payorder['type'], ['shop_hb', 'restaurant_takeaway_hb'])) {
			//合并支付订单重新计算
			// 查询同笔订单信息
			$child_order = Db::name('payorder')->where('aid', $payorder['aid'])->where('ordernum', 'like', $payorder['ordernum'] . '_%')->select()->toArray();
			//修改子价格，修改订单号
			if ($child_order) {
				$totalprice = 0;
				$newOrdernumArr = explode('_', $newOrdernum);
				$newOrdernum = $newOrdernumArr[0];
				foreach ($child_order as $v) {
					// 修改订单号
					$order_num = explode('_', $v['ordernum']);
					$new_order_num = str_replace($order_num[0], $newOrdernum, $v['ordernum']);
					$update_arr = ['ordernum' => $new_order_num];
					if ($v['orderid'] == $updateOrderId) {
						$update_arr['money'] = $newprice; //改动后金额
						$totalprice += $newprice;
					} else {
						$totalprice += $v['money'];
						$where = [];
						$where[] = ['aid', '=', $v['aid']];
						$where[] = ['id', '=', $v['orderid']];
						Db::name($v['type'] . '_order')->where($where)->where('id', $v['orderid'])->update($update_arr);
						Db::name($v['type'] . '_order_goods')->where($where)->where('orderid', $v['orderid'])->update($update_arr);
					}
					Db::name('payorder')->where('id', $v['id'])->update($update_arr);
				}

				//修改主价格，修改订单号
				Db::name('payorder')->where('id', $id)->update(['ordernum' => $newOrdernum, 'money' => $totalprice]);
			}
		} else {
			Db::name('payorder')->where('id', $id)->update(['ordernum' => $newOrdernum, 'money' => $newprice]);
		}
	}
	//支付完成后操作
	public static function payorder($orderid, $paytype, $paytypeid, $paynum = '') {
		if (!$orderid) {
			return;
		}

		$payorder = Db::name('payorder')->where('id', $orderid)->find();
		//dump($payorder.'看看支付后的操作');
		if (!$payorder || $payorder['status'] == 1) {
			return ['status' => 0, 'msg' => '该订单已支付'];
		}

		if (getcustom('pay_yuanbao') && $payorder['type'] == 'shop') {
			if ($payorder['is_yuanbao_pay'] == 1) {
				$paytype = t('元宝') . '支付+' . $paytype;
			}
			//元宝 更新shop_order和payorder
			self::yuanbao_up($payorder['type'], $payorder);
		}

		if ($payorder['type'] == 'restaurant_shop' && $paytypeid == 4) {
			Db::name('payorder')->where('id', $orderid)->update(['paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum]);
		} else {
			Db::name('payorder')->where('id', $orderid)->update(['status' => 1, 'paytime' => time(), 'paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum]);
		}
		$type = $payorder['type'];

		if ($type == 'shop_hb') {
			Db::name('shop_order')->where('ordernum', 'like', $payorder['ordernum'] . '%')->update(['status' => 1, 'paytime' => time(), 'paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum, 'platform' => $payorder['platform']]);
		} elseif ($type == 'balance') {
			Db::name('shop_order')->where('id', $payorder['orderid'])->update(['balance_pay_status' => 1, 'balance_pay_orderid' => $orderid]);
			return ['status' => 1, 'msg' => ''];
		} elseif ($type == 'yuyue_balance') {
			Db::name('yuyue_order')->where('id', $payorder['orderid'])->update(['status' => 3, 'balance_pay_status' => 1, 'balance_pay_orderid' => $orderid]);
		} elseif ($type == 'yuyue_addmoney') {
			Db::name('yuyue_order')->where('id', $payorder['orderid'])->update(['addmoneyStatus' => 1]);
		} elseif ($type == 'seckill2') {
			Db::name('seckill2_order')->where('id', $payorder['orderid'])->update(['status' => 1, 'paytime' => time(), 'paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum, 'platform' => $payorder['platform']]);
			$form_sale = Db::name('seckill2_order')->alias('or')->join('seckill2_product pro', 'or.proid = pro.id')->field('pro.saleid')->where('or.ordernum', $payorder['ordernum'])->find();
			$sale = Db::name('seckill2_sale')->where('id', $form_sale['saleid'])->find();
			Db::name('seckill2_order')->where('ordernum', $sale['form_ordernum'])->update(['status' => 10]);
		} elseif ($type == 'form') {
			Db::name($type . '_order')->where('id', $payorder['orderid'])->update(['paystatus' => 1, 'paytime' => time(), 'paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum, 'platform' => $payorder['platform']]);
		} elseif ($type == 'plug_business_pay') {
			Db::name($type . '_order')->where('id', $payorder['orderid'])->update(['status' => 1, 'paytime' => time(), 'paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum, 'platform' => $payorder['platform']]);
		} elseif ($type == 'restaurant_shop' && $paytypeid == 4) {
			Db::name($type . '_order')->where('id', $payorder['orderid'])->update(['paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum, 'platform' => $payorder['platform']]);
		} elseif ($type == 'workorder') {
			//工单订单
			Db::name($type . '_order')->where('id', $payorder['orderid'])->update(['paystatus' => 1, 'paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum, 'platform' => $payorder['platform']]);
		} else {
			Db::name($type . '_order')->where('id', $payorder['orderid'])->update(['status' => 1, 'paytime' => time(), 'paytype' => $paytype, 'paytypeid' => $paytypeid, 'paynum' => $paynum, 'platform' => $payorder['platform']]);
		}
		if (getcustom('invite_free')) {
			if ($payorder['type'] == 'shop') {
				//发送订单通知
				self::send_free_notice($payorder);
			}
		}
		$fun = $type . '_pay';
		self::$fun($payorder['orderid'], $payorder['ordernum']);
		self::afterusecoupon($payorder['orderid'], $type, 1, $payorder['ordernum']);
		return ['status' => 1, 'msg' => ''];
	}
	//商城订单合并支付
	public static function shop_hb_pay($orderid, $ordernum) {
		$orderlist = Db::name('shop_order')->where('ordernum', 'like', $ordernum . '%')->select()->toArray();
		foreach ($orderlist as $order) {
			self::shop_pay($order['id']);
		}
	}
	//商城订单
	public static function shop_pay($orderid) {
		$order = Db::name('shop_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];

		//逢单奖励时更新奖金数据
		if (getcustom('fengdanjiangli')) {
			$oglist = Db::name('shop_order_goods')->where('orderid', $orderid)->select()->toArray();
			foreach ($oglist as $og) {
				$product = Db::name('shop_product')->where('id', $og['proid'])->find();
				if ($product['fengdanjiangli'] && $product['commissionset'] == 2) {
					$commissiondata = json_decode($product['commissiondata2'], true);
					$fengdanjiangliArr = explode(',', $product['fengdanjiangli']);
					$num = $og['num'];
					$ogupdate = [];
					/*
						$memberlevel = Db::name('member_level')->where('aid',$aid)->where('id',$member['levelid'])->find();
						if($memberlevel['sort'] > 1){
							Db::name('shop_order_goods')->where('id',$og['id'])->update(['isdan'=>1]);
							$dannum = Db::name('shop_order_goods')->where('aid',$aid)->where('proid',$product['id'])->where('status','in','1,2,3')->where("(mid=".$og['parent1']." and isdan=1) or parent1=".$og['parent1'])->sum('num');
							for($i=0;$i<$num;$i++){
								$thisdannum = ($dannum+1+$i)%10;
								if(in_array($thisdannum.'',$fengdanjiangliArr)){
									Db::name('shop_order_goods')->where('id',$og['id'])->update(['isfenhong'=>2]);
									Db::name('member_commission_record')->insert(['aid'=>$aid,'mid'=>$mid,'frommid'=>$mid,'orderid'=>$orderid,'ogid'=>$og['id'],'type'=>'shop','commission'=>$commissiondata[$memberlevel['id']]['commission1'],'remark'=>'复购奖励','createtime'=>time()]);
								}
							}
						}
					*/

					if ($og['parent1']) {
						$parent1 = Db::name('member')->where('id', $og['parent1'])->find();
						$agleveldata1 = Db::name('member_level')->where('aid', $aid)->where('id', $parent1['levelid'])->find();
						$dannum = Db::name('shop_order_goods')->where('aid', $aid)->where('proid', $product['id'])->where('status', 'in', '1,2,3')->where("parent1", $og['parent1'])->sum('num');

						$ogupdate['parent1commission'] = 0;
						for ($i = 0; $i < $num; $i++) {
							$thisdannum = ($dannum + 1 + $i) % 10;
							if (in_array($thisdannum . '', $fengdanjiangliArr)) {
								$ogupdate['parent1commission'] += $commissiondata[$agleveldata1['id']]['commission1'];
								$ogupdate['isfenhong'] = 2;
							}
						}
					}
					if ($og['parent2']) {
						$parent2 = Db::name('member')->where('id', $og['parent2'])->find();
						$agleveldata2 = Db::name('member_level')->where('aid', $aid)->where('id', $parent2['levelid'])->find();
						$dannum = Db::name('shop_order_goods')->where('aid', $aid)->where('proid', $product['id'])->where('status', 'in', '1,2,3')->where("parent2", $og['parent2'])->sum('num');
						$ogupdate['parent2commission'] = 0;
						for ($i = 0; $i < $num; $i++) {
							$thisdannum = ($dannum + 1 + $i) % 10;
							if (in_array($thisdannum . '', $fengdanjiangliArr)) {
								$ogupdate['parent2commission'] += $commissiondata[$agleveldata2['id']]['commission2'];
								$ogupdate['isfenhong'] = 2;
							}
						}
					}
					if ($og['parent3']) {
						$parent3 = Db::name('member')->where('id', $og['parent3'])->find();
						$agleveldata3 = Db::name('member_level')->where('aid', $aid)->where('id', $parent3['levelid'])->find();
						$dannum = Db::name('shop_order_goods')->where('aid', $aid)->where('proid', $product['id'])->where('status', 'in', '1,2,3')->where("parent3", $og['parent3'])->sum('num');
						$ogupdate['parent3commission'] = 0;
						for ($i = 0; $i < $num; $i++) {
							$thisdannum = ($dannum + 1 + $i) % 10;
							if (in_array($thisdannum . '', $fengdanjiangliArr)) {
								$ogupdate['parent3commission'] += $commissiondata[$agleveldata3['id']]['commission3'];
								$ogupdate['isfenhong'] = 2;
							}
						}
					}
					if ($ogupdate) {
						Db::name('shop_order_goods')->where('id', $og['id'])->update($ogupdate);
						if ($og['parent1'] && $ogupdate['parent1commission']) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $og['parent1'], 'frommid' => $og['mid'], 'orderid' => $orderid, 'ogid' => $og['id'], 'type' => 'shop', 'commission' => $ogupdate['parent1commission'], 'remark' => '下级购买商品奖励', 'createtime' => time()]);
						}
						if ($og['parent2'] && $ogupdate['parent2commission']) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $og['parent2'], 'frommid' => $og['mid'], 'orderid' => $orderid, 'ogid' => $og['id'], 'type' => 'shop', 'commission' => $ogupdate['parent2commission'], 'remark' => '下二级购买商品奖励', 'createtime' => time()]);
						}
						if ($og['parent3'] && $ogupdate['parent3commission']) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $og['parent3'], 'frommid' => $og['mid'], 'orderid' => $orderid, 'ogid' => $og['id'], 'type' => 'shop', 'commission' => $ogupdate['parent3commission'], 'remark' => '下三级购买商品奖励', 'createtime' => time()]);
						}
					}
				}
			}
		}
		if (getcustom('to86yk')) {
			$oglist = Db::name('shop_order_goods')->where('orderid', $orderid)->select()->toArray();
			foreach ($oglist as $og) {
				if ($og['to86yk_tid']) {
					$shopset = Db::name('shop_sysset')->where('aid', $aid)->find();
					$formdata = \app\model\Freight::getformdata($orderid, 'shop_order');
					$successnum = 0;
					for ($i = 0; $i < $og['num']; $i++) {
						$url = 'http://api.yukawl.cn/api.php?act=pay&tid=' . $og['to86yk_tid'] . '&input1=' . $formdata[0][1] . '&user=' . $shopset['to86yk_user'] . '&pass=' . $shopset['to86yk_pwd'];
						$rs = request_get($url);
						//\think\facade\Log::write($url);
						\think\facade\Log::write($rs);
						$rs = json_decode($rs, true);
						\think\facade\Log::write($rs);
						if ($rs && $rs['code'] == 0) {
							$successnum++;
						}

					}
					Db::name('shop_order_goods')->where('id', $og['id'])->update(['to86yk_successnum' => $successnum]);
				}
			}
		}

		Db::name('shop_order_goods')->where('orderid', $orderid)->update(['status' => 1]);

		if ($order['fromwxvideo'] == 1) {
			if ($order['paytypeid'] != 60) {
				//不是视频号微信支付的
				Db::name('shop_order')->where('id', $orderid)->update(['fromwxvideo' => 0]);
				$order['fromwxvideo'] = 0;
				//\app\common\Wxvideo::createorder($orderid);
			} else {
				\app\common\Wxvideo::orderpay($orderid);
			}
		}
		//自动发货
		if ($order['freight_type'] == 3) {
			$og = Db::name('shop_order_goods')->where('orderid', $order['id'])->find();
			$freight_content = Db::name('shop_product')->where('id', $og['proid'])->value('freightcontent');
			Db::name('shop_order')->where('id', $order['id'])->update(['freight_content' => $freight_content, 'status' => 2, 'send_time' => time()]);
			Db::name('shop_order_goods')->where('orderid', $order['id'])->update(['status' => 2]);

			if ($order['fromwxvideo'] == 1) {
				\app\common\Wxvideo::deliverysend($orderid);
			}
			if (getcustom('plug_zhiming')) {
				\app\common\Order::collect($order);
				Db::name('shop_order')->where('id', $orderid)->update(['status' => 3, 'collect_time' => time()]);
				Db::name('shop_order_goods')->where('orderid', $orderid)->update(['status' => 3, 'endtime' => time()]);
			}
		}
		//在线卡密
		if ($order['freight_type'] == 4) {
			$og = Db::name('shop_order_goods')->where('orderid', $order['id'])->find();
			$codelist = Db::name('shop_codelist')->where('proid', $og['proid'])->where('status', 0)->order('id')->limit($og['num'])->select()->toArray();
			if ($codelist && count($codelist) >= $og['num']) {
				$pscontent = [];
				foreach ($codelist as $codeinfo) {
					$pscontent[] = $codeinfo['content'];
					Db::name('shop_codelist')->where('id', $codeinfo['id'])->update(['orderid' => $order['id'], 'ordernum' => $order['ordernum'], 'headimg' => $member['headimg'], 'nickname' => $member['nickname'], 'buytime' => time(), 'status' => 1]);
				}
				$pscontent = implode("\r\n", $pscontent);
				Db::name('shop_order')->where('id', $order['id'])->update(['freight_content' => $pscontent, 'status' => 2, 'send_time' => time()]);
				Db::name('shop_order_goods')->where('orderid', $order['id'])->update(['status' => 2]);
			}
			if ($order['fromwxvideo'] == 1) {
				\app\common\Wxvideo::deliverysend($orderid);
			}
			if (getcustom('plug_zhiming')) {
				\app\common\Order::collect($order);
				Db::name('shop_order')->where('id', $orderid)->update(['status' => 3, 'collect_time' => time()]);
				Db::name('shop_order_goods')->where('orderid', $orderid)->update(['status' => 3, 'endtime' => time()]);
			}
		}
		if (getcustom('huawa') && $aid == 1) {
			//定制1 订单对接 同步到花娃
			\app\custom\Huawa::api($order);
		}
		if (getcustom('cefang') && $aid == 2) {
			//定制1 订单对接 同步到策方
			\app\custom\Cefang::api($order);
		}

		if (getcustom('hmy_yuyue')) {
			// 红蚂蚁定制同步到跑腿订单
			\app\custom\Yuyue::api($order);
		}

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'shop', $order['totalprice'], $order['id']);
		if ($couponlist) {
			$proids = db('shop_order_goods')->where('orderid', '=', $order['id'])->column('proid');
			foreach ($couponlist as $coupon) {
				if ($coupon['paygive'] == 1 && $coupon['paygive_minprice'] <= $order['totalprice'] && $coupon['paygive_maxprice'] >= $order['totalprice'] && in_array('shop', explode(',', $coupon['paygive_scene']))) {
					\app\common\Coupon::send($aid, $mid, $coupon['id']);
				}
				if ($coupon['buyprogive'] == 1) {
					$coupon['buyproids'] = explode(',', $coupon['buyproids']);
					$coupon['buypro_give_num'] = explode(',', $coupon['buypro_give_num']);
					foreach ($coupon['buyproids'] as $k => $proid) {
						if (in_array($proid, $proids) && $coupon['buypro_give_num'][$k] > 0) {
							for ($i = 0; $i < $coupon['buypro_give_num'][$k]; $i++) {
								\app\common\Coupon::send($aid, $mid, $coupon['id']);
							}
						}
					}
				}
			}
		}
		//送积分
		if ($order['givescore2'] > 0) {
			\app\common\Member::addscore($aid, $order['mid'], $order['givescore2'], '购买产品赠送' . t('积分'));
		}

		\app\common\Member::uplv($aid, $mid);
		\app\common\Wifiprint::print($aid, 'shop', $order['id']);

		//公众号通知 订单支付成功
		$tmplcontent = [];
		if ($order['paytypeid'] != 4) {
			$tmplcontent['first'] = '有新订单支付成功';
		} else {
			$tmplcontent['first'] = '有新订单下单成功（' . $order['paytype'] . '）';
		}
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/shoporder', $aid), $order['mdid']);

		if ($order['paytypeid'] != 4) {
			$tmplcontent['first'] = '恭喜您的订单已支付成功';
		} else {
			$tmplcontent['first'] = '恭喜您的订单已下单成功';
		}
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('pages/my/usercenter', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		if ($order['paytypeid'] != 4) {
			$tmplcontent['phrase10'] = '已支付';
		} else {
			$tmplcontent['phrase10'] = $order['paytype'];
		}
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/shoporder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);

		$set = Db::name('admin_set')->where('aid', $aid)->find();
		if ($set['fxjiesuantime'] == 1 && $set['fxjiesuantime_delaydays'] == '0') {
			\app\common\Order::giveCommission($order, 'shop');
		}
	}
	//积分商城订单
	public static function scoreshop_pay($orderid) {
		$order = Db::name('scoreshop_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		//自动发货
		if ($order['freight_type'] == 3) {
			$og = Db::name('scoreshop_order_goods')->where('orderid', $order['id'])->find();
			$freight_content = Db::name('scoreshop_product')->where('id', $og['proid'])->value('freightcontent');
			Db::name('scoreshop_order')->where('id', $order['id'])->update(['freight_content' => $freight_content, 'status' => 2, 'send_time' => time()]);
			Db::name('scoreshop_order_goods')->where('orderid', $order['id'])->update(['status' => 2]);
		}
		//在线卡密
		if ($order['freight_type'] == 4) {
			$og = Db::name('scoreshop_order_goods')->where('orderid', $order['id'])->find();
			$codelist = Db::name('scoreshop_codelist')->where('proid', $og['proid'])->where('status', 0)->order('id')->limit($og['num'])->select()->toArray();
			if ($codelist && count($codelist) >= $og['num']) {
				$pscontent = [];
				foreach ($codelist as $codeinfo) {
					$pscontent[] = $codeinfo['content'];
					Db::name('scoreshop_codelist')->where('id', $codeinfo['id'])->update(['orderid' => $order['id'], 'ordernum' => $order['ordernum'], 'headimg' => $member['headimg'], 'nickname' => $member['nickname'], 'buytime' => time(), 'status' => 1]);
				}
				$pscontent = implode("\r\n", $pscontent);
				Db::name('scoreshop_order')->where('id', $order['id'])->update(['freight_content' => $pscontent, 'status' => 2, 'send_time' => time()]);
				Db::name('scoreshop_order_goods')->where('orderid', $order['id'])->update(['status' => 2]);
			}
		}
		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'scoreshop', $order['totalprice']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}
		\app\common\Wifiprint::print($aid, 'scoreshop', $order['id']);
		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新' . t('积分') . '商城订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalscore'] . t('积分') . ($order['totalprice'] > 0 ? ' + ' . $order['totalprice'] . '元' : ''); //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, 0, 'tmpl_orderpay', $tmplcontent, m_url('admin/order/scoreshoporder', $aid), $order['mdid']);

		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('activity/scoreshop/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, 0, 'tmpl_orderpay', $tmplcontent, 'admin/order/scoreshoporder', $order['mdid']);
		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);

		$set = Db::name('admin_set')->where('aid', $aid)->find();
		if ($set['fxjiesuantime'] == 1 && $set['fxjiesuantime_delaydays'] == '0') {
			\app\common\Order::giveCommission($order, 'scoreshop');
		}
	}
	//拼团订单
	public static function collage_pay($orderid) {
		$order = Db::name('collage_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		if ($order['buytype'] != 1) {
			$team = Db::name('collage_order_team')->where('id', $order['teamid'])->find();
			$tdata = [];
			$tdata['num'] = $team['num'] + 1;
			if ($team['num'] + 1 >= $team['teamnum']) {
				$tdata['status'] = 2;
				//团长奖励积分
				$orderlist = Db::name('collage_order')->where(['teamid' => $team['id'], 'status' => 1])->select()->toArray();
				$leader = Db::name('member')->where('id', $team['mid'])->find();
				foreach ($orderlist as $v) {
					if ($v['givescore'] > 0) {
						\app\common\Member::addscore($aid, $v['mid'], $v['givescore'], '购买产品奖励' . t('积分'));
					}
					//自动发货
					if ($order['freight_type'] == 3) {
						$freight_content = Db::name('collage_product')->where('id', $order['proid'])->value('freightcontent');
						Db::name('collage_order')->where('id', $order['id'])->update(['freight_content' => $freight_content, 'status' => 2, 'send_time' => time()]);
					}
					//在线卡密
					if ($order['freight_type'] == 4) {
						$codelist = Db::name('collage_codelist')->where('proid', $order['proid'])->where('status', 0)->order('id')->limit($order['num'])->select()->toArray();
						if ($codelist && count($codelist) >= $order['num']) {
							$pscontent = [];
							foreach ($codelist as $codeinfo) {
								$pscontent[] = $codeinfo['content'];
								Db::name('collage_codelist')->where('id', $codeinfo['id'])->update(['orderid' => $order['id'], 'ordernum' => $order['ordernum'], 'headimg' => $member['headimg'], 'nickname' => $member['nickname'], 'buytime' => time(), 'status' => 1]);
							}
							$pscontent = implode("\r\n", $pscontent);
							Db::name('collage_order')->where('id', $order['id'])->update(['freight_content' => $pscontent, 'status' => 2, 'send_time' => time()]);
						}
					}
					//公众号通知 拼团成功通知
					$tmplcontent = [];
					$tmplcontent['first'] = '有新拼团订单拼团成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $order['title']; //商品名称
					$tmplcontent['keyword2'] = $leader['nickname']; //团长
					$tmplcontent['keyword3'] = $team['teamnum']; //成团人数
					//\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_collagesuccess',$tmplcontent,m_url('admin/order/collageorder'));
					$tmplcontent['first'] = '恭喜您拼团成功';
					$rs = \app\common\Wechat::sendtmpl($aid, $v['mid'], 'tmpl_collagesuccess', $tmplcontent, m_url('activity/collage/orderlist', $aid));
					//订阅消息
					$tmplcontent = [];
					$tmplcontent['thing1'] = $order['title'];
					$tmplcontent['thing10'] = $leader['nickname'];
					$tmplcontent['number12'] = $team['teamnum'];

					$tmplcontentnew = [];
					$tmplcontentnew['thing7'] = $order['title'];
					$tmplcontentnew['thing12'] = $leader['nickname'];
					$tmplcontentnew['number2'] = $team['teamnum'];
					\app\common\Wechat::sendwxtmpl($aid, $v['mid'], 'tmpl_collagesuccess', $tmplcontentnew, 'pages/my/usercenter', $tmplcontent);
					//短信通知
					$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_collagesuccess', ['ordernum' => $order['ordernum']]);
				}
			} else {
				$tdata['status'] = 1;
			}
			Db::name('collage_order_team')->where('aid', $aid)->where('id', $order['teamid'])->update($tdata);
		} else {
			//自动发货
			if ($order['freight_type'] == 3) {
				$freight_content = Db::name('collage_product')->where('id', $order['proid'])->value('freightcontent');
				Db::name('collage_order')->where('id', $order['id'])->update(['freight_content' => $freight_content, 'status' => 2, 'send_time' => time()]);
			}
			//在线卡密
			if ($order['freight_type'] == 4) {
				$codelist = Db::name('collage_codelist')->where('proid', $order['proid'])->where('status', 0)->order('id')->limit($order['num'])->select()->toArray();
				if ($codelist && count($codelist) >= $order['num']) {
					$pscontent = [];
					foreach ($codelist as $codeinfo) {
						$pscontent[] = $codeinfo['content'];
						Db::name('collage_codelist')->where('id', $codeinfo['id'])->update(['orderid' => $order['id'], 'ordernum' => $order['ordernum'], 'headimg' => $member['headimg'], 'nickname' => $member['nickname'], 'buytime' => time(), 'status' => 1]);
					}
					$pscontent = implode("\r\n", $pscontent);
					Db::name('collage_order')->where('id', $order['id'])->update(['freight_content' => $pscontent, 'status' => 2, 'send_time' => time()]);
				}
			}
		}

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'collage', $order['totalprice']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}
		\app\common\Wifiprint::print($aid, 'collage', $order['id']);

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新拼团订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/collageorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('activity/collage/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/collageorder', $order['mdid']);

		//短信通知
		if ($order['buytype'] == 1) {
			//直接购买
			$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
		}
	}
	public static function cycle_pay($orderid) {
		$order = Db::name('cycle_order')->where('id', $orderid)->find();

		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		Db::name('cycle_order_stage')->where('aid', $aid)->where('orderid', $order['id'])->update(['status' => 1]);

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'cycle', $order['totalprice']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}
		\app\common\Wifiprint::print($aid, 'cycle', $order['id']);

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新周期购订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/cycleorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('pagesExt/cycle/orderList', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/cycleorder', $order['mdid']);

		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);

	}
	//幸运拼团订单
	public static function lucky_collage_pay($orderid) {

		$order = Db::name('lucky_collage_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		if ($order['buytype'] != 1) {
			//支付成功后奖励分享者余额，积分，佣金，优惠券
			if (getcustom('plug_luckycollage')) {
				if ($order['shareid'] > 0) {
					//取出分享
					$pmember = Db::name('member')->where('id', $order['shareid'])->find();
					if ($pmember && $order['sharemoney'] > 0) {
						//1 奖励余额
						\app\common\Member::addmoney($aid, $order['shareid'], $order['sharemoney'], '推荐奖励' . t('余额'));
					} else if ($order['sharescore'] > 0) {
						//2 奖励积分
						\app\common\Member::addscore($aid, $order['shareid'], $order['sharescore'], '推荐奖励' . t('积分'));
					} else if ($order['sharecommission'] > 0) {
						//3 奖励佣金
						\app\common\Member::addcommission($aid, $order['shareid'], $order['mid'], $order['sharecommission'], '推荐奖励' . t('佣金'));
					} elseif ($order['share_yhqids']) {
						//奖励优惠券
						foreach ($order['share_yhqids'] as $yhqid) {
							\app\common\Coupon::send($aid, $order['shareid'], $yhqid);
						}
					}
				}
				if ($order['buytype'] == 2) {
					//减掉开团次数
					if ($order['isjiqiren'] != 1 && $member['ktnum'] > 0) {
						$ktnum = $member['ktnum'] - 1;
						Db::name('member')->where(['aid' => $aid, 'id' => $order['mid']])->update(['ktnum' => $ktnum]);
					}
				}
			}
			$team = Db::name('lucky_collage_order_team')->where('id', $order['teamid'])->find();
			$tdata = [];
			$tdata['num'] = $team['num'] + 1;
			if ($team['num'] + 1 >= $team['teamnum']) {
				$tdata['status'] = 2;
				Db::name('lucky_collage_order_team')->where('aid', $aid)->where('id', $order['teamid'])->update($tdata);
				\app\model\LuckyCollage::kaijiang($order);
			} else {
				$tdata['status'] = 1;
				Db::name('lucky_collage_order_team')->where('aid', $aid)->where('id', $order['teamid'])->update($tdata);
			}
		}

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'lucky_collage', $order['totalprice']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新拼团订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/luckycollageorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $order['mid'], 'tmpl_orderpay', $tmplcontent, m_url('activity/luckycollage/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/luckycollageorder', $order['mdid']);

		\app\common\Wifiprint::print($aid, 'collage', $order['id']);
		//短信通知
		if ($order['buytype'] == 1) {
			//直接购买
			$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
		}
	}

	//砍价订单
	public static function kanjia_pay($orderid) {
		$order = Db::name('kanjia_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$product = Db::name('kanjia_product')->where('id', $order['proid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		//自动发货
		if ($order['freight_type'] == 3) {
			$freight_content = $product['freightcontent'];
			Db::name('collage_order')->where('id', $order['id'])->update(['freight_content' => $freight_content, 'status' => 2, 'send_time' => time()]);
		}
		//在线卡密
		if ($order['freight_type'] == 4) {
			$codelist = Db::name('kanjia_codelist')->where('proid', $order['proid'])->where('status', 0)->order('id')->limit($order['num'])->select()->toArray();
			if ($codelist && count($codelist) >= $order['num']) {
				$pscontent = [];
				foreach ($codelist as $codeinfo) {
					$pscontent[] = $codeinfo['content'];
					Db::name('kanjia_codelist')->where('id', $codeinfo['id'])->update(['orderid' => $order['id'], 'ordernum' => $order['ordernum'], 'headimg' => $member['headimg'], 'nickname' => $member['nickname'], 'buytime' => time(), 'status' => 1]);
				}
				$pscontent = implode("\r\n", $pscontent);
				Db::name('kanjia_order')->where('id', $order['id'])->update(['freight_content' => $pscontent, 'status' => 2, 'send_time' => time()]);
			}
		}

		//买单后发放帮砍得积分或余额
		if ($product['helpgive_ff'] == 1 && $product['helpgive_percent'] > 0 && $order['joinid']) {
			$helplist = Db::name('kanjia_help')->where('aid', $aid)->where('joinid', $order['joinid'])->where('mid', '<>', $mid)->select()->toArray();
			foreach ($helplist as $help) {
				if ($product['helpgive_type'] == 0) {
					//积分
					$givescore = intval($help['cut_price'] * $product['helpgive_percent'] * 0.01);
					if ($givescore > 0) {
						\app\common\Member::addscore($aid, $help['mid'], $givescore, '帮好友砍价奖励');
					}
				}
				if ($product['helpgive_type'] == 1) {
					//余额
					$givemoney = round($help['cut_price'] * $product['helpgive_percent'] * 0.01, 2);
					if ($givemoney > 0) {
						\app\common\Member::addmoney($aid, $help['mid'], $givemoney, '帮好友砍价奖励');
					}
				}
			}
		}
		//下单增加帮砍次数
		if ($product['perhelpnum_buyadd'] > 0) {
			$sharelog = Db::name('kanjia_sharelog')->where('aid', $aid)->where('proid', $product['id'])->where('mid', $mid)->find();
			if ($sharelog) {
				Db::name('kanjia_sharelog')->where('id', $sharelog['id'])->inc('addtimes', $product['perhelpnum_buyadd'])->update();
			} else {
				$data = [];
				$data['aid'] = $aid;
				$data['proid'] = $product['id'];
				$data['mid'] = $mid;
				$data['addtimes'] = $product['perhelpnum_buyadd'];
				Db::name('kanjia_sharelog')->insert($data);
			}
		}
		//下单送抽奖/余额/积分/优惠券
		if ($product['paygive_choujiangtimes'] > 0 && $product['paygive_choujiangid'] > 0) {
			$sharelog = Db::name('choujiang_sharelog')->where('aid', $aid)->where('hid', $product['paygive_choujiangid'])->where('mid', $mid)->find();
			if ($sharelog) {
				Db::name('choujiang_sharelog')->where('id', $sharelog['id'])->inc('extratimes', $product['paygive_choujiangtimes'])->update();
			} else {
				$data = [];
				$data['aid'] = $aid;
				$data['hid'] = $product['paygive_choujiangid'];
				$data['mid'] = $mid;
				$data['extratimes'] = $product['paygive_choujiangtimes'];
				Db::name('choujiang_sharelog')->insert($data);
			}
		}
		if ($product['paygive_money'] > 0) {
			\app\common\Member::addmoney($aid, $mid, $product['paygive_money'], '砍价活动下单赠送');
		}
		if ($product['paygive_score'] > 0) {
			\app\common\Member::addscore($aid, $mid, $product['paygive_score'], '砍价活动下单赠送');
		}
		if ($product['paygive_couponid'] > 0) {
			\app\common\Coupon::send($aid, $mid, $product['paygive_couponid']);
		}

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'collage', $order['totalprice']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}
		\app\common\Wifiprint::print($aid, 'kanjia', $order['id']);

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新砍价订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/kanjiaorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('activity/kanjia/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/kanjiaorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}
	//秒杀订单
	public static function seckill_pay($orderid) {
		$order = Db::name('seckill_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		//自动发货
		if ($order['freight_type'] == 3) {
			$freight_content = Db::name('seckill_product')->where('id', $order['proid'])->value('freightcontent');
			Db::name('seckill_order')->where('id', $order['id'])->update(['freight_content' => $freight_content, 'status' => 2, 'send_time' => time()]);
		}
		//在线卡密
		if ($order['freight_type'] == 4) {
			$codelist = Db::name('seckill_codelist')->where('proid', $order['proid'])->where('status', 0)->order('id')->limit($order['num'])->select()->toArray();
			if ($codelist && count($codelist) >= $order['num']) {
				$pscontent = [];
				foreach ($codelist as $codeinfo) {
					$pscontent[] = $codeinfo['content'];
					Db::name('seckill_codelist')->where('id', $codeinfo['id'])->update(['orderid' => $order['id'], 'ordernum' => $order['ordernum'], 'headimg' => $member['headimg'], 'nickname' => $member['nickname'], 'buytime' => time(), 'status' => 1]);
				}
				$pscontent = implode("\r\n", $pscontent);
				Db::name('seckill_order')->where('id', $order['id'])->update(['freight_content' => $pscontent, 'status' => 2, 'send_time' => time()]);
			}
		}

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'seckill', $order['totalprice']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}
		\app\common\Wifiprint::print($aid, 'seckill', $order['id']);

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新秒杀订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/seckillorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('activity/seckill/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/seckillorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);

		$set = Db::name('admin_set')->where('aid', $aid)->find();
		if ($set['fxjiesuantime'] == 1 && $set['fxjiesuantime_delaydays'] == '0') {
			\app\common\Order::giveCommission($order, 'seckill');
		}
	}

	//充值订单
	public static function recharge_pay($orderid) {
		$order = Db::name('recharge_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];

		\app\common\Member::addmoney($aid, $mid, $order['money'], t('余额') . '充值');
		//充值赠送
		$giveset = Db::name('recharge_giveset')->where('aid', $aid)->find();
		if ($giveset && $giveset['status'] == 1) {
			$givedata = json_decode($giveset['givedata'], true);
		} else {
			$givedata = array();
		}
		$givemoney = 0;
		$givescore = 0;
		$moneyduan = 0;
		if ($givedata) {
			foreach ($givedata as $give) {
				if ($order['money'] * 1 >= $give['money'] * 1 && $give['money'] * 1 > $moneyduan) {
					$moneyduan = $give['money'] * 1;
					$givemoney = $give['give'] * 1;
					$givescore = $give['give_score'] * 1;
				}
			}
		}
		if ($givemoney > 0) {
			\app\common\Member::addmoney($aid, $mid, $givemoney, '充值赠送');
		}
		if ($givescore > 0) {
			\app\common\Member::addscore($aid, $mid, $givescore, '充值赠送');
		}

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'recharge', $order['money']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}

		//升级
		\app\common\Member::uplv($aid, $mid);

		$tmplcontent = array();
		$tmplcontent['first'] = '有新充值订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['accountType'] = t('会员') . '昵称';
		$tmplcontent['account'] = $member['nickname'];
		$tmplcontent['amount'] = $order['money'] . '元' . ($givemoney > 0 ? '+赠送' . $givemoney . '元' : '');
		$tmplcontent['result'] = '充值成功';
		\app\common\Wechat::sendhttmpl($aid, 0, 'tmpl_recharge', $tmplcontent, m_url('admin/finance/rechargelog', $aid), $order['mdid']);

		//充值通知
		if (getcustom('zhaopin')) {
			\app\model\Zhaopin::sendhtsms('tmpl_recharge', [], $order['aid'], $order['bid'], $order['mdid']);
		}
	}

	//会员升级订单
	public static function member_levelup_pay($orderid) {
		$order = Db::name('member_levelup_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];

		//成为分销商
		$leveldata = Db::name('member_level')->where('aid', $aid)->where('id', $order['levelid'])->find();
		if ($leveldata['apply_check']) {
			//$return = array('status'=>2,'msg'=>'付款成功请等待审核');

			$tmplcontent = [];
			$tmplcontent['first'] = '有新用户申请升级,请及时处理';
			$tmplcontent['remark'] = '请进入电脑端后台进行审核~';
			$tmplcontent['keyword1'] = $leveldata['name']; //会员等级
			$tmplcontent['keyword2'] = '待审核'; //审核状态
			\app\common\Wechat::sendhttmpl($aid, 0, 'tmpl_uplv', $tmplcontent);

		} else {
			if ($leveldata['yxqdate'] > 0) {
				$levelendtime = strtotime(date('Y-m-d')) + 86400 + 86400 * $leveldata['yxqdate'];
			} else {
				$levelendtime = 0;
			}
			Db::name('member_levelup_order')->where('id', $orderid)->update(['status' => 2, 'paytime' => time()]);
			//判断是否默认分组
			if ($leveldata['cid'] > 0) {
				$is_default = Db::name('member_level_category')->where('id', $leveldata['cid'])->value('isdefault');
			}

			if ($is_default || $leveldata['cid'] == 0) {
				Db::name('member')->where('aid', $aid)->where('id', $mid)->update(['levelid' => $leveldata['id'], 'levelendtime' => $levelendtime]);
				if (getcustom('level_comwithdraw')) {
					if ($member['levelstarttime'] <= 0 && $leveldata['fenhong'] > 0) {
						Db::name('member')->where('aid', $aid)->where('id', $mid)->update(['levelstarttime' => time()]);
					}
				} else {
					Db::name('member')->where('aid', $aid)->where('id', $mid)->update(['levelstarttime' => time()]);
				}

				//更新代理区域
				if ($leveldata['areafenhong'] == 1) {
					Db::name('member')->where('aid', $aid)->where('id', $order['mid'])->update(['areafenhong_province' => $order['areafenhong_province']]);
				} elseif ($leveldata['areafenhong'] == 2) {
					Db::name('member')->where('aid', $aid)->where('id', $order['mid'])->update(['areafenhong_province' => $order['areafenhong_province'], 'areafenhong_city' => $order['areafenhong_city']]);
				} elseif ($leveldata['areafenhong'] == 3) {
					Db::name('member')->where('aid', $aid)->where('id', $order['mid'])->update(['areafenhong_province' => $order['areafenhong_province'], 'areafenhong_city' => $order['areafenhong_city'], 'areafenhong_area' => $order['areafenhong_area']]);
				} elseif ($leveldata['areafenhong'] == 10) {
					Db::name('member')->where('aid', $aid)->where('id', $order['mid'])->update(['areafenhong_largearea' => $order['areafenhong_largearea']]);
				}
			} else {
				if (getcustom('plug_sanyang')) {
					$count = Db::name('member_level_record')->where('aid', $aid)->where('mid', $mid)->where('cid', $leveldata['cid'])->count();
					if ($count) {
						Db::name('member_level_record')->where('aid', $aid)->where('mid', $mid)->where('cid', $leveldata['cid'])->update(['levelid' => $leveldata['id'], 'levelendtime' => $levelendtime]);
					} else {
						$record_data = ['levelid' => $leveldata['id'], 'levelendtime' => $levelendtime];
						$record_data['aid'] = $aid;
						$record_data['mid'] = $mid;
						$record_data['createtime'] = time();
						$record_data['cid'] = $leveldata['cid'];
						Db::name('member_level_record')->insertGetId($record_data);
					}
					if (getcustom('level_comwithdraw')) {
						if ($member['levelstarttime'] <= 0 && $leveldata['fenhong'] > 0) {
							Db::name('member_level_record')->where('aid', $aid)->where('mid', $mid)->where('cid', $leveldata['cid'])->update(['levelstarttime' => time()]);
						}
					} else {
						Db::name('member_level_record')->where('aid', $aid)->where('mid', $mid)->where('cid', $leveldata['cid'])->update(['levelstarttime' => time()]);
					}

					//更新代理区域
					if ($leveldata['areafenhong'] == 1) {
						Db::name('member_level_record')->where('aid', $aid)->where('mid', $mid)->where('cid', $leveldata['cid'])->update(['areafenhong_province' => $order['areafenhong_province']]);
					} elseif ($leveldata['areafenhong'] == 2) {
						Db::name('member_level_record')->where('aid', $aid)->where('mid', $mid)->where('cid', $leveldata['cid'])->update(['areafenhong_province' => $order['areafenhong_province'], 'areafenhong_city' => $order['areafenhong_city']]);
					} elseif ($leveldata['areafenhong'] == 3) {
						Db::name('member_level_record')->where('aid', $aid)->where('mid', $mid)->where('cid', $leveldata['cid'])->update(['areafenhong_province' => $order['areafenhong_province'], 'areafenhong_city' => $order['areafenhong_city'], 'areafenhong_area' => $order['areafenhong_area']]);
					}
				}
			}

			//0702给上级更新拉新时间
			if (getcustom('seckill2') && in_array($order['levelid'], explode(',', Db::name('seckill2_sysset')->where('aid', $order['aid'])->value('seckill_level')))) {
				Db::name('member')->where(['aid' => $aid, 'id' => $member['pid']])->update(['laxin_time' => time()]);
			}

			if ($leveldata['apply_payfenxiao'] == 1) {
				//升级费用参与分销及分红
				\app\common\Common::applypayfenxiao($aid, $order['id']);
			}

			//赠送积分
			if ($leveldata['up_give_score'] > 0) {
				\app\common\Member::addscore($aid, $mid, $leveldata['up_give_score'], '升级奖励');
			}

			//奖励佣金
			if ($leveldata['up_give_commission'] > 0) {
				\app\common\Member::addcommission($aid, $mid, 0, $leveldata['up_give_commission'], '升级奖励');
			}

			//奖励余额
			if ($leveldata['up_give_money'] > 0) {
				\app\common\Member::addmoney($aid, $mid, $leveldata['up_give_money'], '升级奖励');
			}

			//赠送上级佣金
			if ($leveldata['up_give_parent_money'] > 0) {
				$pid = Db::name('member')->where('aid', $aid)->where('id', $mid)->value('pid');
				if ($pid > 0) {
					\app\common\Member::addcommission($aid, $pid, $mid, $leveldata['up_give_parent_money'], '直推奖');
				}

			}

			$tmplcontent = [];
			$tmplcontent['first'] = '恭喜您成功升级为' . $leveldata['name'];
			$tmplcontent['remark'] = '点击进入查看~';
			$tmplcontent['keyword1'] = $leveldata['name']; //会员等级
			$tmplcontent['keyword2'] = '已生效'; //审核状态
			$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_uplv', $tmplcontent, m_url('pages/my/usercenter', $aid));

		}
	}
	//表单支付
	public static function form_pay($orderid) {
		$order = Db::name('form_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		//分销提成
		$form = Db::name('form')->where('id', $order['formid'])->find();
		$totalcommission = 0;
		if ($form && $form['commissionset'] != -1) {
			$ogdata = [];
			if ($member['pid']) {
				$parent1 = Db::name('member')->where('aid', $aid)->where('id', $member['pid'])->find();
				if ($parent1) {
					$agleveldata1 = Db::name('member_level')->where('aid', $aid)->where('id', $parent1['levelid'])->find();
					if ($agleveldata1['can_agent'] != 0) {
						$ogdata['parent1'] = $parent1['id'];
					}
				}
			}
			if ($parent1['pid']) {
				$parent2 = Db::name('member')->where('aid', $aid)->where('id', $parent1['pid'])->find();
				if ($parent2) {
					$agleveldata2 = Db::name('member_level')->where('aid', $aid)->where('id', $parent2['levelid'])->find();
					if ($agleveldata2['can_agent'] > 1) {
						$ogdata['parent2'] = $parent2['id'];
					}
				}
			}
			if ($parent2['pid']) {
				$parent3 = Db::name('member')->where('aid', $aid)->where('id', $parent2['pid'])->find();
				if ($parent3) {
					$agleveldata3 = Db::name('member_level')->where('aid', $aid)->where('id', $parent3['levelid'])->find();
					if ($agleveldata3['can_agent'] > 2) {
						$ogdata['parent3'] = $parent3['id'];
					}
				}
			}
			if ($form['commissionset'] == 1) {
//按比例
				$commissiondata = json_decode($form['commissiondata1'], true);
				if ($commissiondata) {
					$ogdata['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $order['money'] * 0.01;
					$ogdata['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $order['money'] * 0.01;
					$ogdata['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $order['money'] * 0.01;
				}
			} elseif ($form['commissionset'] == 2) {
//按固定金额
				$commissiondata = json_decode($form['commissiondata2'], true);
				if ($commissiondata) {
					$ogdata['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * 1;
					$ogdata['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * 1;
					$ogdata['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * 1;
				}
			} else {
				$ogdata['parent1commission'] = $agleveldata1['commission1'] * $order['money'] * 0.01;
				$ogdata['parent2commission'] = $agleveldata2['commission2'] * $order['money'] * 0.01;
				$ogdata['parent3commission'] = $agleveldata3['commission3'] * $order['money'] * 0.01;
			}

			if ($ogdata['parent1'] && $ogdata['parent1commission'] > 0) {
				$totalcommission += $ogdata['parent1commission'];
				\app\common\Member::addcommission($aid, $ogdata['parent1'], $mid, $ogdata['parent1commission'], '下级购买奖励');
			}
			if ($ogdata['parent2'] && $ogdata['parent2commission'] > 0) {
				$totalcommission += $ogdata['parent2commission'];
				\app\common\Member::addcommission($aid, $ogdata['parent2'], $mid, $ogdata['parent2commission'], '下二级购买奖励');
			}
			if ($ogdata['parent3'] && $ogdata['parent3commission'] > 0) {
				$totalcommission += $ogdata['parent3commission'];
				\app\common\Member::addcommission($aid, $ogdata['parent3'], $mid, $ogdata['parent3commission'], '下三级购买奖励');
			}
			if ($ogdata['parent1']) {
				\app\common\Member::uplv($aid, $ogdata['parent1']);
			}
		}

		if ($order['bid'] != 0) {
//入驻商家的货款
			$binfo = Db::name('business')->where('aid', $aid)->where('id', $order['bid'])->find();
			$totalmoney = $order['money'] - $totalcommission;
			if ($totalmoney > 0) {
				$totalmoney = $totalmoney * (100 - $binfo['feepercent']) * 0.01;
			}
			\app\common\Business::addmoney($aid, $order['bid'], $totalmoney, '表单支付 订单号：' . $order['ordernum']);
		}

		$tmplcontent = [];
		$tmplcontent['first'] = '有客户提交表单成功';
		$tmplcontent['remark'] = '点击查看详情~';
		$tmplcontent['keyword1'] = $order['title'];
		$tmplcontent['keyword2'] = date('Y-m-d H:i');
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_formsub', $tmplcontent, m_url('admin/form/formdetail?id=' . $orderid, $aid));
	}

	//工单支付
	public static function workorder_pay($orderid) {
		$order = Db::name('workorder_order')->where('id', $orderid)->find();
		$aid = $order['aid'];

		$tmplcontent = [];
		$tmplcontent['first'] = '有客户提交工单成功';
		$tmplcontent['remark'] = '请进入电脑端后台进行查看~';
		$tmplcontent['keyword1'] = $order['title'];
		$tmplcontent['keyword2'] = date('Y-m-d H:i');
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_formsub', $tmplcontent);
	}

	//付费查看页面
	public static function designerpage_pay($orderid) {
		$order = Db::name('designerpage_order')->where('id', $orderid)->find();
		\app\common\Order::collect($order, 'designerpage');
	}
	//购买优惠券
	public static function coupon_pay($orderid) {
		$order = Db::name('coupon_order')->where('id', $orderid)->find();
		\app\common\Coupon::send($order['aid'], $order['mid'], $order['cpid']);
		\app\common\Order::collect($order, 'coupon');
	}
	//买单
	public static function maidan_pay($orderid) {
		$order = Db::name('maidan_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		if ($order['couponrid']) {
			Db::name('coupon_record')->where('id', $order['couponrid'])->update(['status' => 1, 'usetime' => time()]);
		}
		\app\common\Order::collect($order, 'maidan');

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'maidan', $order['money']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}
		if (getcustom('everyday_hongbao')) {
			$hd = Db::name('hongbao_everyday')->where('aid', $aid)->find();
			if ($hd['status'] == 1 && $hd['hongbao_bl_maidan'] > 0) {
				$hongbaoEdu = round($order['paymoney'] * $hd['maidan_hongbao_bl'] / 100, 2);
				if ($hongbaoEdu > 0) {
					\app\common\Member::addHongbaoEverydayEdu($aid, $order['mid'], $hongbaoEdu, '买单增加红包额度');
				}

			}
		}
		if (getcustom('maidan_qrcode')) {
			\app\custom\MaidanQrcode::deal_ycommission($order, $member);
		}
		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新买单订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['money'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/maidanlog', $aid), $order['mdid']);

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/maidanlog', $order['mdid']);
	}

	//餐饮订单合并支付
	public static function restaurant_takeaway_hb_pay($orderid, $ordernum) {
		$orderlist = Db::name('restaurant_takeaway_order')->where('ordernum', 'like', $ordernum . '%')->select()->toArray();
		foreach ($orderlist as $order) {
			self::restaurant_takeaway_pay($order['id']);
		}
	}
	//餐饮订单
	public static function restaurant_takeaway_pay($orderid) {
		$order = Db::name('restaurant_takeaway_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		Db::name('restaurant_takeaway_order_goods')->where('orderid', $orderid)->update(['status' => 1]);
		$takeaway_set = Db::name('restaurant_takeaway_sysset')->where('aid', $aid)->where('bid', $order['bid'])->find();
		if ($takeaway_set['confirm_auto'] == 1) {
			//自动接单
			Db::name('restaurant_takeaway_order')->where('id', $orderid)->update(['status' => 12]);
			Db::name('restaurant_takeaway_order_goods')->where('orderid', $orderid)->update(['status' => 12]);
		}

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'restaurant', $order['totalprice'], $order['id']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}

		\app\common\Member::uplv($aid, $mid);
		\app\custom\Restaurant::print('restaurant_takeaway', $order);

		if ($takeaway_set['confirm_auto'] == 1) {
			//自动接单
			//判断是否自动派单
			if ($order['freight_type'] == 2) {
				$peisong_set = \db('peisong_set')->where('aid', $aid)->find();
				if ($peisong_set['express_wx_status'] == 1 && $peisong_set['express_wx_paidan'] == 1) {
					Db::name('restaurant_takeaway_order')->where('id', $orderid)->update(['express_type' => 'express_wx']);
					\app\custom\ExpressWx::addOrder('restaurant_takeaway_order', $order);
				}
			}
		}

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/restaurant/takeawayorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('restaurant/takeaway/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/restaurant/takeawayorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}
	//餐饮订单
	public static function restaurant_booking_pay($orderid) {
		$order = Db::name('restaurant_booking_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		Db::name('restaurant_booking_order_goods')->where('orderid', $orderid)->update(['status' => 1]);

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'restaurant', $order['totalprice'], $order['id']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}

		\app\common\Member::uplv($aid, $mid);
		\app\custom\Restaurant::print('restaurant_booking', $order);

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新预定支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/restaurant/bookingorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的预定订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('restaurant/booking/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/restaurant/bookingorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}

	//餐饮订单
	public static function restaurant_shop_pay($orderid) {
		$order = Db::name('restaurant_shop_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$bid = $order['bid'];
		$mid = $order['mid'];
		Db::name('restaurant_shop_order_goods')->where('orderid', $orderid)->update(['status' => 1]);

		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'restaurant', $order['totalprice'], $order['id']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}

		\app\common\Member::uplv($aid, $mid);
		//根据餐后付款设置，开启时下单后打印小票，关闭时付款后打印小票
		$restaurant_shop_sysset = Db::name('restaurant_shop_sysset')->where('aid', $aid)->where('bid', $bid)->find();
		if ($restaurant_shop_sysset['pay_after'] == 0) {
			\app\custom\Restaurant::print('restaurant_shop', $order);
		}

		//公众号通知 订单支付成功
		$tmplcontent = [];
		if ($order['paytypeid'] != 4) {
			$tmplcontent['first'] = '有新点餐订单支付成功';
		} else {
			$tmplcontent['first'] = '有新点餐订单下单成功（线下支付），';
		}
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/restaurant/shoporder', $aid), $order['mdid']);
		if ($order['paytypeid'] != 4) {
			$tmplcontent['first'] = '恭喜您的点餐订单已支付成功';
		} else {
			$tmplcontent['first'] = '恭喜您的点餐订单下单成功';
		}
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('restaurant/shop/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/restaurant/shoporder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}
	public static function plug_businessqr_pay_pay($orderid) {
		$order = Db::name('plug_businessqr_pay_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		if ($order['bid'] != 0) {
//入驻商家的货款
			$binfo = Db::name('business')->where('aid', $aid)->where('id', $order['bid'])->find();
			$totalmoney = $order['cost_price'];
			\app\common\Business::addmoney($aid, $order['bid'], $totalmoney, '会员支付，订单号：' . $order['ordernum']);
			//店铺加销量
			Db::name('business')->where('aid', $aid)->where('id', $order['bid'])->inc('sales', 1)->update();

			//公众号通知 订单支付成功
			$tmplcontent = [];
			$tmplcontent['first'] = '有新会员支付订单支付成功';
			$tmplcontent['remark'] = '点击进入查看~';
			$tmplcontent['keyword1'] = $member['nickname']; //用户名
			$tmplcontent['keyword2'] = $order['ordernum']; //订单号
			$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
			$tmplcontent['keyword4'] = $order['title']; //商品信息
			\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/finance/bmoneylog', $aid));
			$tmplcontent['first'] = '恭喜您已支付成功';
			$rs = \app\common\Wechat::sendtmpl($aid, $order['mid'], 'tmpl_orderpay', $tmplcontent, m_url('pages/money/moneylog?st=1', $aid));

			$tmplcontent = [];
			$tmplcontent['thing11'] = $order['title'];
			$tmplcontent['character_string2'] = $order['ordernum'];
			$tmplcontent['phrase10'] = '已支付';
			$tmplcontent['amount13'] = $order['totalprice'] . '元';
			$tmplcontent['thing27'] = $member['nickname'];
			\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/finance/bmoneylog');
		}
	}
	//预约服务支付
	public static function yuyue_pay($orderid) {
		//var_dump($orderid);
		$order = Db::name('yuyue_order')->where('id', $orderid)->find();
		$aid = $order['aid'];
		$member = Db::name('member')->where('id', $order['mid'])->find();
		if (!$order) {
			return json(['status' => 0, 'msg' => '订单不存在']);
		}

		if ($order['status'] != 1 && $order['status'] != 12) {
			return json(['status' => 0, 'msg' => '订单状态不符合']);
		}

		if (getcustom('hmy_yuyue') && in_array($order['paidan_type'], [2, 3])) {
			//定制 将订单同步到师傅app端
			\app\custom\Yuyue::apiyuyue($order);
		} else {
			$yyset = Db::name('yuyue_set')->where('aid', $aid)->find();
			if ($yyset['paidantype'] == 0 && $yyset['isautopd'] == 1) {
				//自动派单到大厅
				$rs = \app\model\YuyueWorkerOrder::create($order, 0, '');
			}
			if ($order['worker_id']) {
				//如果用户已经选择服务人员则支付后直接进行派单
				$rs = \app\model\YuyueWorkerOrder::create($order, $order['worker_id'], '');
				if ($rs['status'] == 0) {
					return json($rs);
				}

				//\app\common\System::plog('预约派单'.$orderid);
			}
		}
		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新预约订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/yuyueorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $order['mid'], 'tmpl_orderpay', $tmplcontent, m_url('activity/yuyue/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/yuyueorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}

	//预约尾款支付
	public static function yuyue_balance_pay($orderid) {
		//var_dump($orderid);
		$order = Db::name('yuyue_order')->where('id', $orderid)->find();
		$aid = $order['aid'];
		$member = Db::name('member')->where('id', $order['mid'])->find();
		if (!$order) {
			return json(['status' => 0, 'msg' => '订单不存在']);
		}

		if ($order['balance_pay_status'] != 1) {
			return json(['status' => 0, 'msg' => '订单状态不符合']);
		}

		if (getcustom('hmy_yuyue') && $order['sysOrderNo']) {
			//将订单同步到师傅app端
			//直接完成订单
			db('yuyue_order')->where(['aid' => $order['aid'], 'id' => $orderid])->update(['status' => 3, 'collect_time' => time()]);
			$rs = \app\common\Order::collect($order, 'yuyue');
			Db::name('yuyue_worker_order')->where('id', $order['worker_orderid'])->update(['status' => 3, 'endtime' => time()]);
			\app\custom\Yuyue::payoff($order);
		}
		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '尾款订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/yuyueorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $order['mid'], 'tmpl_orderpay', $tmplcontent, m_url('activity/yuyue/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/yuyueorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}

	//课程支付
	public static function kecheng_pay($orderid) {
		//var_dump($orderid);
		$order = Db::name('kecheng_order')->where('id', $orderid)->find();
		$aid = $order['aid'];
		//增加学习人数
		Db::name('kecheng_list')->where('aid', $order['aid'])->where('id', $order['kcid'])->inc('join_num')->update();
		if (!$order) {
			return json(['status' => 0, 'msg' => '订单不存在']);
		}

		if ($order['status'] != 1 && $order['status'] != 12) {
			return json(['status' => 0, 'msg' => '订单状态不符合']);
		}

		$member = Db::name('member')->where('aid', $order['aid'])->where('id', $order['mid'])->find();
		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新课程订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['price'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/kechengorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $order['mid'], 'tmpl_orderpay', $tmplcontent, m_url('activity/kecheng/orderlist?bid=' . $order['bid'], $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/kechengorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
		\app\common\Order::collect($order, 'kecheng');
	}

	//团购
	public static function tuangou_pay($orderid) {
		$order = Db::name('tuangou_order')->where('id', $orderid)->find();
		$member = Db::name('member')->where('id', $order['mid'])->find();
		$aid = $order['aid'];
		$mid = $order['mid'];
		//自动发货
		if ($order['freight_type'] == 3) {
			Db::name('tuangou_order')->where('id', $order['id'])->update(['status' => 2, 'send_time' => time()]);
		}
		//在线卡密
		if ($order['freight_type'] == 4) {
			$codelist = Db::name('tuangou_codelist')->where('proid', $order['proid'])->where('status', 0)->order('id')->limit($order['num'])->select()->toArray();
			if ($codelist && count($codelist) >= $order['num']) {
				$pscontent = [];
				foreach ($codelist as $codeinfo) {
					$pscontent[] = $codeinfo['content'];
					Db::name('tuangou_codelist')->where('id', $codeinfo['id'])->update(['orderid' => $order['id'], 'ordernum' => $order['ordernum'], 'headimg' => $member['headimg'], 'nickname' => $member['nickname'], 'buytime' => time(), 'status' => 1]);
				}
				$pscontent = implode("\r\n", $pscontent);
				Db::name('tuangou_order')->where('id', $order['id'])->update(['freight_content' => $pscontent, 'status' => 2, 'send_time' => time()]);
			}
		}
		//支付后送券
		$couponlist = \app\common\Coupon::getpaygive($aid, $mid, 'tuangou', $order['totalprice']);
		if ($couponlist) {
			foreach ($couponlist as $coupon) {
				\app\common\Coupon::send($aid, $mid, $coupon['id']);
			}
		}
		\app\common\Wifiprint::print($aid, 'tuangou', $order['id']);
		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新团购订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/tuangouorder', $aid), $order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $mid, 'tmpl_orderpay', $tmplcontent, m_url('activity/tuangou/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/tuangouorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);

		$set = Db::name('admin_set')->where('aid', $aid)->find();
		if ($set['fxjiesuantime'] == 1 && $set['fxjiesuantime_delaydays'] == '0') {
			\app\common\Order::giveCommission($order, 'tuangou');
		}
	}

	//约课服务支付
	public static function yueke_pay($orderid) {
		$order = Db::name('yueke_order')->where('id', $orderid)->find();
		$aid = $order['aid'];
		$member = Db::name('member')->where('id', $order['mid'])->find();
		if (!$order) {
			return json(['status' => 0, 'msg' => '订单不存在']);
		}

		if ($order['status'] != 1 && $order['status'] != 12) {
			return json(['status' => 0, 'msg' => '订单状态不符合']);
		}

		$workerinfo = Db::name('yueke_worker')->where('aid', $aid)->where('workerid', $order['workerid'])->find();

		//公众号通知 订单支付成功
		$tmplcontent = [];
		$tmplcontent['first'] = '有新预约订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum']; //订单号
		$tmplcontent['keyword3'] = $order['totalprice'] . '元'; //订单金额
		$tmplcontent['keyword4'] = $order['title']; //商品信息
		\app\common\Wechat::sendhttmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, m_url('admin/order/yuekeorder', $aid), $order['mdid']);
		if ($workerinfo && $workerinfo['mid']) {
			$rs = \app\common\Wechat::sendtmpl($aid, $workerinfo['mid'], 'tmpl_orderpay', $tmplcontent, m_url('pagesExt/yueke/workerorderlist', $aid));
		}
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid, $order['mid'], 'tmpl_orderpay', $tmplcontent, m_url('pagesExt/yueke/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['totalprice'] . '元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid, $order['bid'], 'tmpl_orderpay', $tmplcontent, 'admin/order/yuekeorder', $order['mdid']);

		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}

	public function business_recharge_pay($orderid) {
		$order = Db::name('business_recharge_order')->where('id', $orderid)->find();
		$info = Db::name('business')->where('id', $order['bid'])->find();
		\app\common\Business::addmoney($order['aid'], $order['bid'], $order['money'], t('余额') . '充值');
		//\app\common\System::plog('给商户充值'.$order['bid']);
	}
	public function yuyue_workerapply_pay($orderid) {
		$order = Db::name('yuyue_workerapply_order')->where('id', $orderid)->find();
		$info = Db::name('yuyue_worker')->where('id', $order['bid'])->find();
		//入驻成功给管理员发通知
		$tmplcontent = [];
		$tmplcontent['first'] = '有师傅申请成功';
		$tmplcontent['remark'] = '请登录后台，查看申请详情~';
		$tmplcontent['keyword1'] = '预约师傅申请';
		$tmplcontent['keyword2'] = date('Y-m-d H:i');
		\app\common\Wechat::sendhttmpl(aid, $info['bid'], 'tmpl_formsub', $tmplcontent, '');
	}

	//预约补余款支付
	public static function yuyue_addmoney_pay($orderid) {
		//var_dump($orderid);
		$order = Db::name('yuyue_order')->where('id', $orderid)->find();
		$aid = $order['aid'];
		$member = Db::name('member')->where('id', $order['mid'])->find();
		if (!$order) {
			return json(['status' => 0, 'msg' => '订单不存在']);
		}

		if ($order['addmoneyStatus'] != 1) {
			return json(['status' => 0, 'msg' => '订单状态不符合']);
		}

		//公众号通知 订单支付成功
		/*$tmplcontent = [];
		$tmplcontent['first'] = '补余款订单支付成功';
		$tmplcontent['remark'] = '点击进入查看~';
		$tmplcontent['keyword1'] = $member['nickname']; //用户名
		$tmplcontent['keyword2'] = $order['ordernum'];//订单号
		$tmplcontent['keyword3'] = $order['addmoney'].'元';//订单金额
		$tmplcontent['keyword4'] = $order['title'];//商品信息
		\app\common\Wechat::sendhttmpl($aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/yuyueorder', $aid),$order['mdid']);
		$tmplcontent['first'] = '恭喜您的订单已支付成功';
		$rs = \app\common\Wechat::sendtmpl($aid,$order['mid'],'tmpl_orderpay',$tmplcontent,m_url('activity/yuyue/orderlist', $aid));

		$tmplcontent = [];
		$tmplcontent['thing11'] = $order['title'];
		$tmplcontent['character_string2'] = $order['ordernum'];
		$tmplcontent['phrase10'] = '已支付';
		$tmplcontent['amount13'] = $order['addmoney'].'元';
		$tmplcontent['thing27'] = $member['nickname'];
		\app\common\Wechat::sendhtwxtmpl($aid,$order['bid'],'tmpl_orderpay',$tmplcontent,'admin/order/yuyueorder',$order['mdid']);
		*/
		//短信通知
		$rs = \app\common\Sms::send($aid, $member['tel'] ? $member['tel'] : $order['tel'], 'tmpl_orderpay', ['ordernum' => $order['ordernum']]);
	}
	//元宝 更新shop_order和payorder
	static private function yuanbao_up($type, $payorder) {

		//元宝支付
		if (getcustom('pay_yuanbao') && $type == 'shop') {
			//如果是元宝支付，则需要重置支付金额
			if ($payorder['is_yuanbao_pay'] == 1) {
				//更新pay_order
				$up_pay = Db::name('payorder')->where('id', $payorder['id'])->update(['money' => $payorder['yuanbao_money']]);

				//查询订单
				$order = Db::name('shop_order')->where('id', $payorder['orderid'])->field('aid,ordernum,mid,total_yuanbao')->find();
				if ($order) {
					//更新shop_order
					$up_order = Db::name('shop_order')->where('id', $payorder['orderid'])->update(['is_yuanbao_pay' => 1, 'yuanbao_money' => $payorder['yuanbao_money'], 'totalprice' => $payorder['yuanbao_money']]);
					//更新商品id
					$sel_goods = Db::name('shop_order_goods')
						->where('orderid', $payorder['orderid'])
						->select()
						->toArray();
					if ($sel_goods) {
						//查询系统设置
						$sysset = Db::name('admin_set')->where('aid', $order['aid'])->find();
						//查询商城设置
						$shopset = Db::name('shop_sysset')->where('aid', $order['aid'])->field('showjd,comment,showcommission,hide_sales,hide_stock,show_lvupsavemoney')->find();
						//查询下单者
						$member = Db::name('member')->where('id', $order['mid'])->find();
						self::deal_commission($order['aid'], $order, $sel_goods, $sysset, $shopset, $member);
					}
				}
				//扣除元宝
				\app\common\Member::addyuanbao($order['aid'], $order['mid'], -$order['total_yuanbao'], '支付订单：' . $order['ordernum']);
			}
		}
	}
	//元宝转账
	static private function member_yuanbao_transfer_pay($id) {

		//元宝支付
		if (getcustom('pay_yuanbao')) {

			//查询订单
			$order = Db::name('member_yuanbao_transfer_order')->where('id', $id)->field('aid,mid,to_mid,money,yuanbao,parent1,parent2,parent3,parent1commission,parent2commission,parent3commission,iscommission')->find();
			$aid = $order['aid'];
			if ($order) {
				$member = Db::name('member')->where('id', $order['mid'])->field('nickname')->find();
				$to_member = Db::name('member')->where('id', $order['to_mid'])->field('nickname')->find();
				if ($member) {
					//直接转账
					$rs = \app\common\Member::addyuanbao($aid, $order['to_mid'], $order['yuanbao'], sprintf("来自%s的" . t('元宝') . "转赠", $member["nickname"]));
					if ($rs['status'] == 1) {
						\app\common\Member::addyuanbao($aid, $order['mid'], $order['yuanbao'] * -1, sprintf(t('元宝') . "转赠给：%s", $to_member['nickname']));
					}
				}

				if ($order['iscommission'] != 1) {
					$totalcommission = 0;
					//发奖
					if ($order['parent1'] && $order['parent1commission'] > 0) {
						$totalcommission += $order['parent1commission'];
						\app\common\Member::addcommission($aid, $order['parent1'], $order['mid'], $order['parent1commission'], '下级' . t('元宝') . '转账奖励');
					}
					if ($order['parent2'] && $order['parent2commission'] > 0) {
						$totalcommission += $order['parent2commission'];
						\app\common\Member::addcommission($aid, $order['parent2'], $order['mid'], $order['parent2commission'], '下二级' . t('元宝') . '转账奖励');
					}
					if ($order['parent3'] && $order['parent3commission'] > 0) {
						$totalcommission += $order['parent3commission'];
						\app\common\Member::addcommission($aid, $order['parent3'], $order['mid'], $order['parent3commission'], '下三级' . t('元宝') . '转账奖励');
					}

					//更新发佣金状态
					$up = Db::name('member_yuanbao_transfer_order')->where('id', $id)->update(['iscommission' => 1]);
				}
			}
		}
	}
	//重新计算佣金
	static private function deal_commission($aid, $order, $sel_goods, $sysset, $shopset, $member) {
		//元宝支付
		if (getcustom('pay_yuanbao')) {
			if ($sysset) {
				$yuanbao_money_ratio = $sysset['yuanbao_money_ratio'] / 100;
			} else {
				$yuanbao_money_ratio = 0;
			}
			foreach ($sel_goods as $ogdata) {

				//计算商品元宝现金价格
				$yuanbao_money = $ogdata['total_yuanbao'] * $yuanbao_money_ratio;
				$yuanbao_money = round($yuanbao_money, 2);
				//更新商品金额
				$up_goods = Db::name('shop_order_goods')->where('id', $ogdata['id'])->update(['yuanbao_money' => $yuanbao_money, 'totalprice' => $yuanbao_money, 'real_totalprice' => $yuanbao_money]);

				//删除之前的会员佣金记录
				Db::name('member_commission_record')->where('orderid', $ogdata['orderid'])->where('ogid', $ogdata['id'])->delete();

				//查询规格
				$guige = Db::name('shop_guige')->where('aid', $aid)->where('id', $ogdata['ggid'])->find();

				//实际支付价格
				$og_totalprice = $yuanbao_money;

				//数量
				$num = $ogdata['num'];
				//佣金总价格
				$commission_totalprice = 0;

				if ($sysset['fxjiesuantype'] == 1) {
					//按成交价格
					$commission_totalprice = $yuanbao_money;
					if ($commission_totalprice < 0) {
						$commission_totalprice = 0;
					}
				}
				if ($sysset['fxjiesuantype'] == 2) {
					//按销售利润
					$commission_totalprice = $yuanbao_money - $guige['cost_price'] * $num;
					if ($commission_totalprice < 0) {
						$commission_totalprice = 0;
					}
				}
				$commission_totalpriceCache = $commission_totalprice;

				$ogupdate = [];
				$ogupdate['parent1'] = 0;
				$ogupdate['parent2'] = 0;
				$ogupdate['parent3'] = 0;
				$ogupdate['parent4'] = 0;

				$ogupdate['parent1commission'] = 0;
				$ogupdate['parent2commission'] = 0;
				$ogupdate['parent3commission'] = 0;
				$ogupdate['parent4commission'] = 0;

				$ogupdate['parent1score'] = 0;
				$ogupdate['parent2score'] = 0;
				$ogupdate['parent3score'] = 0;

				$ogupdate['hongbaoEdu'] = 0;
				$ogupdate['business_total_money'] = 0;
				//自己是否拿一级分成
				$agleveldata = Db::name('member_level')->where('aid', $aid)->where('id', $member['levelid'])->find();
				if ($agleveldata['can_agent'] > 0 && $agleveldata['commission1own'] == 1) {
					$member['pid'] = $ogdata['mid'];
				}

				//查询商品
				$product = Db::name('shop_product')->where('id', $ogdata['proid'])->find();

				if ($product['bid'] > 0) {

					$store_info = Db::name('business')->where('aid', $aid)->where('id', $product['bid'])->find();

					$totalprice_business = $og_totalprice;
					//商品独立费率
					if ($product['feepercent'] != '' && $product['feepercent'] != null && $product['feepercent'] >= 0) {
						$ogupdate['business_total_money'] = $totalprice_business * (100 - $product['feepercent']) * 0.01;
					} else {
						//商户费率
						$ogupdate['business_total_money'] = $totalprice_business * (100 - $store_info['feepercent']) * 0.01;
					}

				}

				if ($product['commissionset'] != -1) {

					if ($member['pid']) {
						$parent1 = Db::name('member')->where('aid', $aid)->where('id', $member['pid'])->find();
						if ($parent1) {
							$agleveldata1 = Db::name('member_level')->where('aid', $aid)->where('id', $parent1['levelid'])->find();
							if ($agleveldata1['can_agent'] != 0) {
								$ogupdate['parent1'] = $parent1['id'];
							}
						}
					}
					if ($parent1['pid']) {
						$parent2 = Db::name('member')->where('aid', $aid)->where('id', $parent1['pid'])->find();
						if ($parent2) {
							$agleveldata2 = Db::name('member_level')->where('aid', $aid)->where('id', $parent2['levelid'])->find();
							if ($agleveldata2['can_agent'] > 1) {
								$ogupdate['parent2'] = $parent2['id'];
							}
						}
					}
					if ($parent2['pid']) {
						$parent3 = Db::name('member')->where('aid', $aid)->where('id', $parent2['pid'])->find();
						if ($parent3) {
							$agleveldata3 = Db::name('member_level')->where('aid', $aid)->where('id', $parent3['levelid'])->find();
							if ($agleveldata3['can_agent'] > 2) {
								$ogupdate['parent3'] = $parent3['id'];
							}
						}
					}
					if ($parent3['pid']) {
						$parent4 = Db::name('member')->where('aid', $aid)->where('id', $parent3['pid'])->find();
						if ($parent4) {
							$agleveldata4 = Db::name('member_level')->where('aid', $aid)->where('id', $parent4['levelid'])->find();
							//持续推荐奖励
							if ($agleveldata4['can_agent'] > 0 && ($agleveldata4['commission_parent'] > 0 || ($parent4['levelid'] == $parent3['levelid'] && $agleveldata4['commission_parent_pj'] > 0))) {
								$ogupdate['parent4'] = $parent4['id'];
							}
						}
					}
					if ($product['commissionset'] == 1) {
//按商品设置的分销比例
						$commissiondata = json_decode($product['commissiondata1'], true);
						if ($commissiondata) {
							if ($agleveldata1) {
								$ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $commission_totalprice * 0.01;
							}

							if ($agleveldata2) {
								$ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $commission_totalprice * 0.01;
							}

							if ($agleveldata3) {
								$ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $commission_totalprice * 0.01;
							}

						}
					} elseif ($product['commissionset'] == 2) {
//按固定金额
						$commissiondata = json_decode($product['commissiondata2'], true);
						if ($commissiondata) {
							if (getcustom('fengdanjiangli') && $product['fengdanjiangli']) {

							} else {
								if ($agleveldata1) {
									$ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
								}

								if ($agleveldata2) {
									$ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
								}

								if ($agleveldata3) {
									$ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
								}

							}
						}
					} elseif ($product['commissionset'] == 3) {
//提成是积分
						$commissiondata = json_decode($product['commissiondata3'], true);
						if ($commissiondata) {
							if ($agleveldata1) {
								$ogupdate['parent1score'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
							}

							if ($agleveldata2) {
								$ogupdate['parent2score'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
							}

							if ($agleveldata3) {
								$ogupdate['parent3score'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
							}

						}
					} else {
						//按会员等级设置的分销比例
						if ($agleveldata1) {
							if (getcustom('plug_ttdz') && $ogdata['isfg'] == 1) {
								$agleveldata1['commission1'] = $agleveldata1['commission4'];
							}
							if ($agleveldata1['commissiontype'] == 1) {
								//固定金额按单
								if ($istc1 == 0) {
									$ogupdate['parent1commission'] = $agleveldata1['commission1'];
									$istc1 = 1;
								}
							} else {
								$ogupdate['parent1commission'] = $agleveldata1['commission1'] * $commission_totalprice * 0.01;
							}
						}
						if ($agleveldata2) {
							if (getcustom('plug_ttdz') && $ogdata['isfg'] == 1) {
								$agleveldata2['commission2'] = $agleveldata2['commission5'];
							}
							if ($agleveldata2['commissiontype'] == 1) {
								if ($istc2 == 0) {
									$ogupdate['parent2commission'] = $agleveldata2['commission2'];
									$istc2 = 1;
									//持续推荐奖励
									if ($agleveldata2['commission_parent'] > 0) {
										$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $agleveldata2['commission_parent'];
									}
									if ($agleveldata1['id'] == $agleveldata2['id'] && $agleveldata2['commission_parent_pj'] > 0) {
										$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $agleveldata2['commission_parent_pj'];
									}
								}
							} else {
								$ogupdate['parent2commission'] = $agleveldata2['commission2'] * $commission_totalprice * 0.01;
								//持续推荐奖励
								if ($agleveldata2['commission_parent'] > 0 && $ogupdate['parent1commission'] > 0) {
									$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $ogupdate['parent1commission'] * $agleveldata2['commission_parent'] * 0.01;
								}
								if ($agleveldata1['id'] == $agleveldata2['id'] && $agleveldata2['commission_parent_pj'] > 0 && $ogupdate['parent1commission'] > 0) {
									$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $ogupdate['parent1commission'] * $agleveldata2['commission_parent_pj'] * 0.01;
								}
							}
						}
						if ($agleveldata3) {
							if (getcustom('plug_ttdz') && $ogdata['isfg'] == 1) {
								$agleveldata3['commission3'] = $agleveldata3['commission6'];
							}
							if ($agleveldata3['commissiontype'] == 1) {
								if ($istc3 == 0) {
									$ogupdate['parent3commission'] = $agleveldata3['commission3'];
									$istc3 = 1;
									//持续推荐奖励
									if ($agleveldata3['commission_parent'] > 0) {
										$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $agleveldata3['commission_parent'];
									}
									if ($agleveldata2['id'] == $agleveldata3['id'] && $agleveldata3['commission_parent_pj'] > 0) {
										$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $agleveldata3['commission_parent_pj'];
									}
								}
							} else {
								$ogupdate['parent3commission'] = $agleveldata3['commission3'] * $commission_totalprice * 0.01;
								//持续推荐奖励
								if ($agleveldata3['commission_parent'] > 0 && $ogupdate['parent2commission'] > 0) {
									$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $ogupdate['parent2commission'] * $agleveldata3['commission_parent'] * 0.01;
								}
								if ($agleveldata2['id'] == $agleveldata3['id'] && $agleveldata3['commission_parent_pj'] > 0 && $ogupdate['parent2commission'] > 0) {
									$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $ogupdate['parent2commission'] * $agleveldata3['commission_parent_pj'] * 0.01;
								}
							}
						}
						//持续推荐奖励
						if ($agleveldata4['commission_parent'] > 0) {
							if ($agleveldata3['commissiontype'] == 1) {
								$ogupdate['parent4commission'] = $agleveldata4['commission_parent'];
							} else {
								$ogupdate['parent4commission'] = $ogupdate['parent3commission'] * $agleveldata4['commission_parent'] * 0.01;
							}
						}
						if ($agleveldata3['id'] == $agleveldata4['id'] && $agleveldata4['commission_parent_pj'] > 0) {
							if ($agleveldata3['commissiontype'] == 1) {
								$ogupdate['parent4commission'] = $agleveldata4['commission_parent_pj'];
							} else {
								$ogupdate['parent4commission'] = $ogupdate['parent3commission'] * $agleveldata4['commission_parent_pj'] * 0.01;
							}
						}
					}
				}
				if ($ogupdate) {
					Db::name('shop_order_goods')->where('id', $ogdata['id'])->update($ogupdate);
				}

				if ($product['commissionset4'] == 1 && $product['lvprice'] == 1) {
					//极差分销

					if ($member['path']) {
						$parentList = Db::name('member')->where('id', 'in', $member['path'])->order(Db::raw('field(id,' . $member['path'] . ')'))->select()->toArray();
						if ($parentList) {
							$parentList = array_reverse($parentList);
							$lvprice_data = json_decode($guige['lvprice_data'], true);
							$nowprice = $commission_totalpriceCache;
							$giveidx = 0;
							foreach ($parentList as $k => $parent) {
								if ($parent['levelid'] && $lvprice_data[$parent['levelid']]) {
									$thisprice = floatval($lvprice_data[$parent['levelid']]) * $num;
									if ($nowprice > $thisprice) {
										$commission = $nowprice - $thisprice;
										$nowprice = $thisprice;
										$giveidx++;
										//添加新的
										Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $parent['id'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $commission, 'score' => 0, 'remark' => '下级购买商品差价', 'createtime' => time()]);
									}
								}
							}
						}
					}
				}

				if ($product['commissionset'] != 4) {
					if (getcustom('plug_ttdz') && $ogdata['isfg'] == 1) {
						if ($ogupdate['parent1'] && ($ogupdate['parent1commission'] || $ogupdate['parent1score'])) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $ogupdate['parent1'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $ogupdate['parent1commission'], 'score' => $ogupdate['parent1score'], 'remark' => '下级复购奖励', 'createtime' => time()]);
						}
						if ($ogupdate['parent2'] && ($ogupdate['parent2commission'] || $ogupdate['parent2score'])) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $ogupdate['parent2'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $ogupdate['parent2commission'], 'score' => $ogupdate['parent2score'], 'remark' => '下二级复购奖励', 'createtime' => time()]);
						}
						if ($ogupdate['parent3'] && ($ogupdate['parent3commission'] || $ogupdate['parent3score'])) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $ogupdate['parent3'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $ogupdate['parent3commission'], 'score' => $ogupdate['parent3score'], 'remark' => '下三级复购奖励', 'createtime' => time()]);
						}
					} else {
						if ($ogupdate['parent1'] && ($ogupdate['parent1commission'] || $ogupdate['parent1score'])) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $ogupdate['parent1'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $ogupdate['parent1commission'], 'score' => $ogupdate['parent1score'], 'remark' => '下级购买商品奖励', 'createtime' => time()]);
						}
						if ($ogupdate['parent2'] && ($ogupdate['parent2commission'] || $ogupdate['parent2score'])) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $ogupdate['parent2'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $ogupdate['parent2commission'], 'score' => $ogupdate['parent2score'], 'remark' => '下二级购买商品奖励', 'createtime' => time()]);
						}
						if ($ogupdate['parent3'] && ($ogupdate['parent3commission'] || $ogupdate['parent3score'])) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $ogupdate['parent3'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $ogupdate['parent3commission'], 'score' => $ogupdate['parent3score'], 'remark' => '下三级购买商品奖励', 'createtime' => time()]);
						}
						if ($ogupdate['parent4'] && ($ogupdate['parent4commission'])) {
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $ogupdate['parent4'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $ogupdate['parent4commission'], 'score' => 0, 'remark' => '持续推荐奖励', 'createtime' => time()]);
						}
					}
					if ($order['checkmemid'] && $commission_totalprice > 0) {
						$checkmember = Db::name('member')->where('aid', $aid)->where('id', $order['checkmemid'])->find();
						if ($checkmember) {
							$buyselect_commission = Db::name('member_level')->where('id', $checkmember['levelid'])->value('buyselect_commission');
							$checkmemcommission = $buyselect_commission * $commission_totalprice * 0.01;
							Db::name('member_commission_record')->insert(['aid' => $aid, 'mid' => $checkmember['id'], 'frommid' => $ogdata['mid'], 'orderid' => $ogdata['orderid'], 'ogid' => $ogdata['id'], 'type' => 'shop', 'commission' => $checkmemcommission, 'score' => 0, 'remark' => '购买商品时指定奖励', 'createtime' => time()]);
						}
					}
				}

				if (getcustom('everyday_hongbao')) {

					$hd = Db::name('hongbao_everyday')->where('aid', $aid)->find();
					$hongbaoEdu = 0;
					if ($product['everyday_hongbao_bl'] === null) {
						$hongbaoEdu = $og_totalprice * $hd['shop_product_hongbao_bl'] / 100;
					} elseif ($product['everyday_hongbao_bl'] > 0) {
						$hongbaoEdu = $og_totalprice * $product['everyday_hongbao_bl'] / 100;
					}
					$hongbaoEdu = round($hongbaoEdu, 2);
					if ($hongbaoEdu > 0) {
						Db::name('shop_order_goods')->where('id', $ogupdate['id'])->update(['hongbaoEdu' => $hongbaoEdu]);
					}
				}
			}
			unset($sg_v);
		}

	}
	public static function cashier_pay($orderid) {
		//如果是多商家且余额支付，则结算商家费用
		$order = Db::name('cashier_order')->where('id', $orderid)->find();
		$totalmoney = $order['totalprice'];
		if ($order['paytypeid'] == 1 && $order['bid'] > 0 && $totalmoney > 0) {
			//商家费率
			$feepercent = Db::name('business')->where('aid', $order['aid'])->where('id', $order['bid'])->value('feepercent');
			if ($feepercent) {
				$totalmoney = $totalmoney * (100 - $feepercent) * 0.01;
			}
			//结算给商家
			$res = \app\common\Business::addmoney($order['aid'], $order['bid'], $totalmoney, '收银台收款');
			if (!$res || $res['status'] != 1) {
				\think\facade\Log::write('--------------商家结算失败--------------');
				\think\facade\Log::write(json_encode($res));
				\think\facade\Log::write('--------------商家结算失败--------------');
			}
		}
		return true;
	}
	public static function dscj_pay($orderid) {
		$order = Db::name('dscj_order')->where('id', $orderid)->find();
		//其他订单关闭
		$where = [];
		$where[] = ['status', '=', 0];
		$where[] = ['bid', '=', $order['bid']];
		$where[] = ['aid', '=', $order['aid']];
		$where[] = ['hid', '=', $order['hid']];
		$where[] = ['mid', '=', $order['mid']];
		Db::name('dscj_order')->where($where)->update(['status' => 4]);
		Db::name('dscj')->where('id', $order['hid'])->inc('joinnum', 1)->update();
		return true;
	}
	//招聘置顶
	public static function zhaopin_top_pay($orderid) {
		if (getcustom('zhaopin')) {
			$order = Db::name('zhaopin_top_order')->where('id', $orderid)->find();
			$zhaopinupdate = [];
			if ($order['status'] == 1 && $order['related_id']) {
				$starttime = $order['paytime'];
				$endtimeS = $order['paytime'];
				$duration = $order['top_duration'];
				$durationTotal = 86400 * $duration;
				$relateinfo = Db::name('zhaopin')->where('aid', $order['aid'])->where('id', $order['related_id'])->find();
				if ($relateinfo) {
					if ($relateinfo['top_endtime'] && $relateinfo['top_endtime'] > time()) {
						$starttime = $relateinfo['top_starttime'];
						$endtimeS = $relateinfo['top_endtime'];
					}
					$zhaopinupdate['top_starttime'] = $starttime;
					$zhaopinupdate['top_endtime'] = $endtimeS + $durationTotal;
					$zhaopinupdate['top_feetype'] = $order['top_feetype'];
					$zhaopinupdate['top_area'] = $order['top_area'];
//                    Db::name('zhaopin')->where('id',$order['related_id'])->update($zhaopinupdate);
				}
			}
			if ($order['assurance_total'] > 0) {
				//同步保证金订单
				$ordera = [];
				$ordera['ordernum'] = $order['ordernum'];
				$ordera['top_orderid'] = $order['id'];
				$ordera['totalprice'] = $order['assurance_total'];
				$ordera['createtime'] = time();
				$ordera['title'] = '担保招聘保证金';
				$ordera['aid'] = $order['aid'];
				$ordera['bid'] = $order['bid'];
				$ordera['mid'] = $order['mid'];
				$ordera['apply_id'] = $order['apply_id'];
				$ordera['status'] = 1;
				$ordera['paynum'] = $order['paynum'];
				$ordera['paytype'] = $order['paytype'];
				$ordera['paytime'] = $order['paytime'];
				Db::name('zhaopin_assurancefee_order')->insertGetId($ordera);

				//保证金累加
				Db::name('zhaopin_apply')->where('aid', $order['aid'])->where('id', $order['apply_id'])->update(['assurance_fee' => Db::raw("assurance_fee+{$order['assurance_total']}")]);

				$data = [];
				$data['createtime'] = time();
				$data['aid'] = $order['aid'];
				$data['bid'] = $order['bid'];
				$data['mid'] = $order['mid'];
				$data['apply_id'] = $order['apply_id'];
				$data['zhaopin_id'] = $order['related_id'];
				$data['status'] = 1; //担保中
				$data['fee'] = $order['assurance_total'];
				$data['remark'] = '置顶担保招聘';
				$assurance_id = Db::name('zhaopin_assurance')->insertGetId($data);
				$zhaopinupdate['assurance_id'] = $assurance_id;
			}
			if ($order['totalprice'] > 0) {
				//发放奖励
				$givescore = \app\model\Zhaopin::getSetValue($order['aid'], 'zhaopin', 'top_give_score', 0);
				if ($givescore > 0) {
					\app\common\Member::addscore($order['aid'], $order['mid'], $givescore, '招聘置顶奖励');
				}
			}
			if ($zhaopinupdate) {
				Db::name('zhaopin')->where('id', $order['related_id'])->update($zhaopinupdate);
			}
		}
		return true;
	}

	//vip
	public static function zhaopin_vip_pay($orderid) {
		if (getcustom('zhaopin')) {
			//其他未支付的订单删除
			$order = Db::name('zhaopin_vip_order')->where('id', $orderid)->find();
			Db::name('zhaopin_vip_order')->where('status', 0)->where('mid', $order['mid'])->where('zhaopin_id', $order['zhaopin_id'])->delete();
			Db::name('zhaopin')->where('aid', $order['aid'])->where('id', $order['zhaopin_id'])->update(['vip_orderid' => $orderid]);
			//vip_order
			Db::name('zhaopin_apply')->where('aid', $order['aid'])->where('mid', $order['mid'])->update(['vip_orderid' => $orderid, 'zhaopin_id' => $order['zhaopin_id']]);
			return;
		}
		return true;
	}

	//求职置顶
	public static function zhaopin_qiuzhi_top_pay($orderid) {
		if (getcustom('zhaopin')) {
			$order = Db::name('zhaopin_qiuzhi_top_order')->where('id', $orderid)->find();
			if ($order['status'] == 1 && $order['related_id']) {
				$starttime = $order['paytime'];
				$endtimeS = $order['paytime'];
				$duration = $order['top_duration'];
				$durationTotal = 86400 * ($duration + 1);
				$relateinfo = Db::name('zhaopin_qiuzhi')->where('aid', $order['aid'])->where('id', $order['related_id'])->find();
				if ($relateinfo) {
					if ($relateinfo['top_endtime'] && $relateinfo['top_endtime'] > time()) {
						$starttime = $relateinfo['top_starttime'];
						$endtimeS = $relateinfo['top_endtime'];
					}
					$update['top_starttime'] = $starttime;
					$update['top_endtime'] = $endtimeS + $durationTotal;
					$update['top_feetype'] = $order['top_feetype'];
					$update['top_area'] = $order['top_area'];
					Db::name('zhaopin_qiuzhi')->where('id', $order['related_id'])->update($update);
				}
			}
			//发放奖励
			$givescore = \app\model\Zhaopin::getSetValue($order['aid'], 'qiuzhi', 'top_give_score', 0);
			if ($givescore > 0) {
				\app\common\Member::addscore($order['aid'], $order['mid'], $givescore, '求职置顶奖励');
			}
		}
		return true;
	}
	//招聘置顶
	public static function zhaopin_assurancefee_pay($orderid) {
		if (getcustom('zhaopin')) {
			$order = Db::name('zhaopin_assurancefee_order')->where('id', $orderid)->find();
			if ($order['apply_id']) {
				//更新商家缴纳保证金的费用
				Db::name('zhaopin_apply')->where('aid', $order['aid'])->where('id', $order['apply_id'])->inc('assurance_fee', $order['totalprice'])->update();
			}
		}
		return true;
	}

	/**
	 * @param $orderid
	 * @param $type
	 * @param $usecoupon_type 1付款后，2确认收货
	 * @param $ordernum
	 * @return bool
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public static function afterusecoupon($orderid, $type, $usecoupon_type = 1, $ordernum = '') {
		//所有使用优惠券的订单类型maidan = couponrid
		if (getcustom('usecoupon_give_score') || getcustom('usecoupon_give_coupon') || getcustom('usecoupon_give_money')) {
			if (in_array($type, [
				'shop', 'shop_hb', 'yuyue', 'tuangou', 'seckill2', 'seckill',
				'restaurant_takeaway', 'restaurant_shop', 'restaurant_booking',
				'maidan', 'lucky_collage', 'dscj', 'collage', 'cashier',
			])) {
				$couponOrders = []; //优惠券字段 全部转为coupon_rid
				//特殊处理的表
				if ($type == 'shop_hb') {
					$couponOrders = Db::name('shop_order')->where('ordernum', 'like', $ordernum . '%')->select()->toArray();
				} elseif ($type == 'maidan') {
					$couponOrders[] = Db::name('maidan_order')->where('id', $orderid)->field('*,couponrid coupon_rid')->find();
				} else {
					$couponOrders[] = Db::name($type . '_order')->where('id', $orderid)->find();
				}
				if (empty($couponOrders)) {
					return true;
				}
				foreach ($couponOrders as $key => $order) {
					if (!isset($order['coupon_rid']) || empty($order['coupon_rid'])) {
						continue;
					}
					\app\common\Coupon::useCoupon($order['aid'], $order['coupon_rid'], $usecoupon_type);
				}
			}
		}

		return true;
	}

	public static function send_free_notice($payorder) {

		if (getcustom('invite_free')) {
			//查询免单设置
			$set = Db::name('invite_free')->where('aid', $payorder['aid'])->find();
			if ($set && $set['status'] == 1 && $set['start_time'] <= time() && $set['end_time'] >= time() && $payorder['aid'] && $payorder['aid'] == 10) {
				//查询订单
				$order = Db::name('shop_order')->where('id', $payorder['orderid'])->field('id,ordernum,mid,title,totalprice,mdid')->find();
				if ($order) {
					//查询他下单次数
					$count_num = Db::name('shop_order')
						->where('mid', $order['mid'])
						->where('status', '>=', 1)
						->where('status', '<=', 3)
						->count();
					if ($count_num == 1) {
						//查询他上级
						$member = Db::name('member')
							->where('id', $order['mid'])
							->field('pid')
							->find();
						if ($member && $member['pid'] > 0) {

							// $tmplcontent['keyword1'] = Db::name('admin_set')->where('aid',aid)->value('name'); //店铺
							// $tmplcontent['thing27'] = $this->member['nickname'];

							//查询通知设置
							$mp_tmplset = Db::name('mp_tmplset')->where('aid', $payorder['aid'])->field('tmpl_orderconfirm')->find();
							if ($mp_tmplset && $mp_tmplset['tmpl_orderconfirm']) {
								//公众号通知 发送有新伙伴下单
								$tmplcontent = [];
								$tmplcontent['first'] = '有新伙伴下单';
								$tmplcontent['remark'] = '点击进入查看~';
								$tmplcontent['keyword1'] = ''; //店铺
								$tmplcontent['keyword2'] = date('Y-m-d H:i:s', $payorder['paytime']); //下单时间
								$tmplcontent['keyword3'] = $order['title'] ? $order['title'] : ''; //商品
								$tmplcontent['keyword4'] = $order['totalprice'] ? $order['totalprice'] : ''; //金额
								\app\common\Wechat::sendtmpl($payorder['aid'], $member['pid'], 'tmpl_orderconfirm', $tmplcontent, m_url('pagesExt/invite_free/index'));
							}

							//查询通知设置
							$wx_tmplset = Db::name('wx_tmplset')->where('aid', $payorder['aid'])->field('tmpl_orderconfirm')->find();
							if ($wx_tmplset && $wx_tmplset['tmpl_orderconfirm']) {
								$tmplcontent = [];
								$tmplcontent['thing11'] = $order['title'] ? $order['title'] : ''; //商品
								$tmplcontent['character_string2'] = $order['ordernum'] ? $order['ordernum'] : '';
								$tmplcontent['phrase10'] = '新伙伴下单';
								$tmplcontent['amount13'] = $order['totalprice'] ? $order['totalprice'] : ''; //金额
								$tmplcontent['thing27'] = '';
								\app\common\Wechat::sendwxtmpl($payorder['aid'], $member['pid'], 'tmpl_orderconfirm', $tmplcontent, m_url('pagesExt/invite_free/index'), $order['mdid']);
							}
						}
					}
				}
			}

		}
	}

	public static function xixie_pay($orderid) {
		if (getcustom('xixie')) {
			$order = Db::name('shop_order')->where('id', $orderid)->find();
			$member = Db::name('member')->where('id', $order['mid'])->find();
			$aid = $order['aid'];
			$mid = $order['mid'];
			Db::name('shop_order_goods')->where('orderid', $orderid)->update(['status' => 1]);
		}
	}

	//充值订单
	public static function xixie_vip_pay($orderid) {
		if (getcustom('xixie')) {
			$order = Db::name('xixie_vip_order')->where('id', $orderid)->find();
			$member = Db::name('member')->where('id', $order['mid'])->update(['is_vip' => 1]);
		}
	}

	public static function article_reward_pay($orderid) {
		if (getcustom('article_reward')) {
			$order = Db::name('article_reward_order')->where('id', $orderid)->find();
			if ($order) {
				//增加打赏
				\app\common\Member::addmoney($order['aid'], $order['send_mid'], $order['num'], '文章打赏');
			}
		}
	}
}
